const Router = require("koa-router")
const {successRender, errorRender, paginationFilter, generateOrderId, generatePickUpNum, getIncome} = require('../utils/utils')
const router = new Router()
const Order = require('../model/Order')
const User = require('../model/User')
const {proving} = require('../utils/token')


//生成订单
router.post('/addOrder', async ctx => {
  const token = ctx.request.header.token
  const orderInfo = ctx.request.body
  const userInfo = await User.find({openId: proving(token).id})
  if (orderInfo.password !== userInfo[0].password) return ctx.body = errorRender({msg: '密码错误'})
  if (userInfo[0].money < orderInfo.price) return ctx.body = errorRender({msg: '余额不足'})
  await User.updateOne({openId: proving(token).id}, {money: userInfo[0].money - orderInfo.price})  //扣钱
  orderInfo.userId = proving(token).id
  orderInfo.orderId = generateOrderId(5)
  orderInfo.pickUpNum = await generatePickUpNum()
  const res = await new Order(orderInfo).save()
  ctx.body = successRender({msg: '支付成功', data: {_id: res._id}})
})

//查询用户订单列表
router.get('/getOrderList', async ctx => {
  const token = ctx.request.header.token
  const userId = proving(token).id
  const res = await Order.find({userId})
  ctx.body = successRender({data: res})
})

//查询全部订单列表
router.get('/getAllOrderList', async ctx => {
  const token = ctx.request.header.token
  const userId = proving(token).id
  const status = ctx.request.query.status
  const keyWord = ctx.request.query.keyWord
  let res
  if (userId) {
    if (keyWord) {
      res = await Order.find({orderId: keyWord})
    } else if (status) {
      res = await Order.find({status})
    } else {
      res = await Order.find()
    }
    const data = paginationFilter(res, ctx.request.query)
    data.income = await getIncome()
    ctx.body = successRender({data})
  } else {
    ctx.body = errorRender({msg: '没有访问权限'})
  }
})

//查询订单详情
router.get('/getOrderDetails', async ctx => {
  const token = ctx.request.header.token
  const _id = ctx.request.query.id
  const res = await Order.findById(_id)
  ctx.body = successRender({data: res})
})

//删除订单
router.get('/removeOrder', async ctx => {
  const token = ctx.request.header.token
  const _id = ctx.request.query.id
  const userId = proving(token).id
  if (userId) {
    const res = await Order.findOneAndRemove({_id})
    ctx.body = successRender({msg: '删除成功'})
  } else {
    ctx.body = errorRender({msg: '没有访问权限'})
  }
})

//完成订单
router.get('/completeOrder', async ctx => {
  const token = ctx.request.header.token
  const _id = ctx.request.query.id
  const userId = proving(token).id
  if (userId) {
    const res = await Order.findByIdAndUpdate(_id, {$set: {status: 1}})
    ctx.body = successRender({msg: '已完成'})
  } else {
    ctx.body = errorRender({msg: '没有访问权限'})
  }
})

//消息中心
router.get('/getMessageList', async ctx => {
  const token = ctx.request.header.token
  const userId = proving(token).id
  if (userId) {
    const res = await Order.find({status: 0})
    ctx.body = successRender({data: res})
  } else {
    ctx.body = errorRender({msg: '没有访问权限'})
  }
})

module.exports = router

